package cn.ffcs.common.web;

import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

import cn.ffcs.util.GetSet;
import cn.ffcs.util.MyList;

/**
 * 实现字典ID与名字的灵活转换<br/>
 * <table>
 * <tr>
 * <td>负责人/principal:</td>
 * <td colspan="2">g.huangjx</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * </tr>
 * <tr>
 * <td>日期:2009-2-26</td>
 * <td>修改人:g.huanjx</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * @author g.huangjx
 * @version $Id$
 */
public class CommonAction {
    public String getValue(){
        ActionContext ctx = ActionContext.getContext();
        HttpServletResponse response = (HttpServletResponse) ctx
                .get(ServletActionContext.HTTP_RESPONSE);
        HttpServletRequest request = (HttpServletRequest) ctx
                .get(ServletActionContext.HTTP_REQUEST);
        String serviceName = request.getParameter("serviceName");
        try {
            Properties pros = new Properties();
            String[] args = {};
            String param = "";// 类方法参数
            if (request.getParameter("args") != null
                    && request.getParameter("args") != "") {
                param = request.getParameter("args");
                param = new String(param.getBytes("ISO-8859-1"), "utf-8");// 编码转换
                args = param.split(",");
            }
            Object returnObject = null;// 定义一个对象实例
            String className = "";
            if (serviceName.indexOf(".") == serviceName.lastIndexOf(".")) {// 从配置文件里获得类名
                java.io.InputStream in = this.getClass().getResourceAsStream(
                        "className.properties");
                pros.load(in);// 加载一个类的实例
                in.close();// 实例加载完毕关闭流
                className = pros.getProperty(serviceName.substring(0,
                        serviceName.indexOf(".")));
            } else {
                className = serviceName.substring(0, serviceName
                        .lastIndexOf("."));// request 过来的参数里获得类名
            }
            GetSet getSet=new GetSet();
            returnObject = getSet.getMethod(Class.forName(className).newInstance(),
                    serviceName.substring(serviceName.lastIndexOf(".") + 1),
                    args);// 访问类的方法
            String str = "";
            if (returnObject != null) {
                str = returnObject.toString();
            }
            str=MyList.getStr(str);
            response.setContentType("text/xml;charset=gb2312");
            response.getWriter().write(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
